package cn.electronic.sale.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.electronic.sale.pojo.Customer;
import cn.electronic.sale.pojo.GoodsCustom;

/**
 * 类描述：消费者控制类
 * 
 * @author: 陈明俊
 * @date： 日期：2016-12-6 时间：下午2:16:49
 * @version 1.0
 */
@Controller
@RequestMapping("/customer")
public class CustomerController extends BasicController {
	

	/**
	 * 操作成功，跳转到首页页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/success")
	public String turnSucces() throws Exception
	{
		return "success";
	}
 	/**
	 * 跳转到个人注册页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/registerUI")
	public String registerUI() throws Exception {
		System.out.println("-----------------------------输出日志：controller/customerCostroller---------");
		return "customer/registration";
	}

	/**
	 * 消费者注册
	 * 
	 * @param customer
	 *            封装消费者信息的实体
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/customerRegister")
	String addJobHunter(Customer customer, String year, String month,
			HttpServletRequest request,HttpSession session) throws Exception {
		customer.setCustomerBirthday(new Date());
		customerService.addCustomer(customer);
		session.setAttribute("customer", customer);
		return "customer/success";
	}
	
	/**
	 * 消费者登录
	 * 
	 * @param customer
	 *            封装消费者信息的实体
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/customerLogin")
	public String loginCustomer(Customer customer,HttpSession session ,HttpServletRequest request) throws Exception{
		System.out.println("---------日志：customer/customerLogin");
		Customer customer2=customerService.loginCustmer(customer);
		session.setAttribute("customer", customer2);	
		return "success";
	}
	
	/**
	 * 跳转到消费者主页，并准备数据
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("indexUI")
	public String indexUI(HttpSession session )throws Exception
	{
		
		System.out.println(session.getAttribute("CustomerController/indexUI"));
		List<GoodsCustom> goodsSearch= goodsService.selectAllGoodsInfo();
		System.out.println(goodsSearch);
		List<GoodsCustom> goodsSearch1 = new ArrayList<GoodsCustom>();
		List<GoodsCustom> goodsSearch2 = new ArrayList<GoodsCustom>();
		List<GoodsCustom> goodsSearch3 = new ArrayList<GoodsCustom>();
		List<GoodsCustom> goodsSearch4 = new ArrayList<GoodsCustom>();
		List<GoodsCustom> goodsSearch5 = new ArrayList<GoodsCustom>();
		List<GoodsCustom> goodsSearch6 = new ArrayList<GoodsCustom>();
		int i=0;
		for (GoodsCustom goodsCustom : goodsSearch) {
			if (i<3) {
				goodsSearch1.add(goodsCustom);
			}else if (i<6) {
				goodsSearch2.add(goodsCustom);
			}else if(i<9) {
				goodsSearch3.add(goodsCustom);
			}else if(i<13){
				goodsSearch4.add(goodsCustom);
			}else if(i<17) {
				goodsSearch5.add(goodsCustom);
			}else {
				goodsSearch6.add(goodsCustom);
			}
			i++;
		}
		Customer customer= (Customer) session.getAttribute("customer");
		session.setAttribute("goodsSearch", goodsSearch);
		session.setAttribute("goodsSearch1", goodsSearch1);
		session.setAttribute("goodsSearch2", goodsSearch2);
		session.setAttribute("goodsSearch3", goodsSearch3);
		session.setAttribute("goodsSearch4", goodsSearch4);
		session.setAttribute("goodsSearch5", goodsSearch5);
		session.setAttribute("goodsSearch6", goodsSearch6);
		
		return "customer/index";
	}
}
